package com.moxing.admin.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;

@Slf4j
@Configuration
@MapperScan(basePackages ={"com.moxing.admin.mapper.Oracle"},sqlSessionTemplateRef = "sqlSessionTemplateOracle")
public class DruidConfigOracle {


    @Bean("dataSourceOracle")
    public DataSource dataSource() {
        //初始化数据源
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl("jdbc:oracle:thin:@//192.168.174.133:1521/ORCLPDB");
        datasource.setDriverClassName("oracle.jdbc.OracleDriver");
        datasource.setInitialSize(10);
        datasource.setMinIdle(5);
        datasource.setMaxActive(20);
        datasource.setMaxWait(1000);
        datasource.setTimeBetweenEvictionRunsMillis(60*1000L);//销毁线程检测间隔（默认60秒）‌
        datasource.setMinEvictableIdleTimeMillis(1000L*60L*30L);//连接最小空闲时间（默认30分钟）‌
        datasource.setMaxEvictableIdleTimeMillis(1000L*60L*30L*7);//连接最大空闲时间（默认7小时）‌
        datasource.setValidationQuery("SELECT 1 FROM DUAL");
        datasource.setTestWhileIdle(false);//空闲时检测（默认false）
        datasource.setTestOnBorrow(true);//获取时检测（默认true）
        datasource.setTestOnReturn(false);//归还时检测（默认false）
        datasource.setPoolPreparedStatements(true);//是否缓存预编译语句‌
        datasource.setMaxPoolPreparedStatementPerConnectionSize(10);//每个连接最大PS缓存数‌
        datasource.setUsername("SYSTEM");
        datasource.setPassword("123456");
        datasource.setKeepAlive(false);//保持连接活性（默认false）
        //配置扩展属性 统计并定义慢 SQL 的时间阈值为 5000 毫秒
        datasource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        try {
            datasource.setFilters("stat");
        } catch (Exception e) {
            log.info("--------------------");
        }
        datasource.setConnectionProperties(null);
        log.info("初始化数据源结束Oracle");
        return datasource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryOracle() throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setDataSource(dataSource());
        factory.setMapperLocations(resolver.getResources("classpath*:/mapper/Oracle/*.xml"));
        // 配置MyBatis
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰命名转换
        configuration.setCallSettersOnNulls(true); // 调用null值的setter
        factory.setConfiguration(configuration);
        return factory.getObject();
    }

    @Bean(name = "sqlSessionTemplateOracle")
    public SqlSessionTemplate sqlSessionTemplateEcs() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryOracle());
    }

}